package Api;

import Util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static Util.Tools.getMD5;

/**
 * 分润可退回余额查询
 */

public class GuaranteeAllotRefundRemainAmtQueryApi {

    public static void main(String[] args) {

        try {
            // 目标URL
            String url = "https://pay.heepay.com/API/Payment/GuaranteeAllotRefundRemainAmtQuery.aspx";
            //分润需要的MD5密钥，商户后台网关密钥下面第一个
            String key = "651886F030134F848F548A12";
            //生成13位时间戳
            long time = System.currentTimeMillis();
            String timestamp = String.valueOf(time);
            //固定版本号
            String version = "3";
            //商户编号，（汇付宝商户内码：七位整数数字）
            String agent_id = "1664502";
            String ref_agent_id = "";
            //支付成功后，汇付宝返回的单据号。
            String jnet_bill_no = "H2406286732815A8";

            //商户系统内部的订单号（要保证唯一）
            String agent_bill_id = "20240628135213";


            String sign_type = "MD5";
            //拼接待加密参数
            StringBuilder builder = new StringBuilder();
            builder.append("version=");
            builder.append(version);
            builder.append("&agent_id=");
            builder.append(agent_id);
            builder.append("&agent_bill_id=");
            builder.append(agent_bill_id);
            builder.append("&ref_agent_id=");
            builder.append(ref_agent_id);
            builder.append("&jnet_bill_no=");
            builder.append(jnet_bill_no);
            builder.append("&timestamp=");
            builder.append(timestamp);
            builder.append("&key=");
            builder.append(key);
            String Date = builder.toString();
            System.out.println("签名前数据:" + Date);
            //MD5加密得出签名结果
            String sign = getMD5(Date);


            // 请求参数，以Map形式表示
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("version", version);
            parameters.put("agent_id", agent_id);
            parameters.put("ref_agent_id", ref_agent_id);
            parameters.put("agent_bill_id", agent_bill_id);
            parameters.put("jnet_bill_no", jnet_bill_no);
            parameters.put("timestamp", timestamp);
            parameters.put("sign_type", sign_type);
            parameters.put("sign", sign);

            String response = HttpUtil.sendPost1(url, parameters);
            System.out.println(response);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}